<?php
/**
 * --------------------------------------------------------------
 * 配置模型
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace app\admin\model;

use think\Model;

class Config extends Model
{
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 获取配置信息
     * @param string $name 配置名
     * @return mixed
     */
    public static function getConfig($name = '')
    {
        $result  = [];
        $configs = self::column('value,type', 'name');
        foreach ($configs as $config) {
            switch ($config['type']) {
                case 'array':
                    $result[$config['name']] = parse_attr($config['value']);
                    break;
                case 'checkbox':
                    $result[$config['name']] = explode(',', $config['value']);
                    break;
                default:
                    $result[$config['name']] = $config['value'];
                    break;
            }
        }

        return $name != '' ? $result[$name] : $result;
    }

    /**
     * 条件查询
     * @param array $map
     * @param string|boolean $field
     * @param string $type
     * @param string $orderby
     * @param string $limit
     * @return array
     */
    public static function queryOneOrList($map = [], $field = true, $type = 'one', $orderby = 'id desc', $limit = '')
    {
        if ($type == 'one') {
            $object = self::where($map)->field($field)->find();
        } elseif ($type == 'list') {
            if (empty($limit)) {
                $object = self::where($map)->field($field)->order($orderby)->select();
            } else {
                $object = self::where($map)->field($field)->order($orderby)->limit($limit)->select();
            }
        }
        
        return $object ? object_to_array($object) : null;
    }
}